<?php
/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2019/9/17
 * Time: 15:25
 */

namespace app\api\controller;


use app\api\ApiController;

class Area extends ApiController
{
    /**
     * 根据pid获取地区信息
     */
    function getAreaByPid(){
        $pid = $this->request->data['parentId'];
        $this->success($this->model->where(['parentId'=>$pid])->select()->toArray());
    }
    function all(){
        $this->success($this->model->select()->toArray());
    }
    function getIdByName(){
        $region = $this->request->data['region'];
        $ids=[];
        $id = 0;
        for ($b=count($region)-1;$b>=0;$b--){
            $area = $this->model->field('id,parentId')->where(['areaName'=>$region[$b]])->select()->toArray();
            if(count($area)==1 && count($ids)==0){
                $id = $area[0]['id'];
                break;
            }else if(count($ids)!=0){
                foreach ($area as $k){
                    foreach ($ids as $k2){
                        if($k2['parentId']==$k['parentId']){
                            $id = $k2['id'];
                            break 3;
                        }
                    }
                }
            }else{
                $ids[] = $area;
            }
        }
        $this->success(['id'=>$id]);
    }
}